/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.crystalrange.documents.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import org.crystalrange.documents.model.DocumentType;
import org.crystalrange.documents.service.DocumentTypeService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author Krzysiek
 */
public class DocumentTypeSelectController extends GenericForwardComposer {

    protected DocumentTypeService documentTypeService;
    protected DocumentType selectedDocumentType;
    protected List<DocumentType> documentTypes;
    protected Search searchDocumentType;
    protected AnnotateDataBinder binder;

    public DocumentTypeService getDocumentTypeService() {
        return documentTypeService;
    }

    public void setDocumentTypeService(DocumentTypeService documentTypeService) {
        this.documentTypeService = documentTypeService;
    }

    public List<DocumentType> getDocumentTypes() {
        return documentTypes;
    }

    public void setDocumentTypes(List<DocumentType> documentTypes) {
        this.documentTypes = documentTypes;
    }

    public DocumentType getSelectedDocumentType() {
        return selectedDocumentType;
    }

    public void setSelectedDocumentType(DocumentType selectedDocumentType) {
        this.selectedDocumentType = selectedDocumentType;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);
        this.searchDocumentType = new Search(DocumentType.class);
        this.searchDocumentType.setMaxResults(100);
        this.searchDocumentType.addFetch("createdBy");
        this.documentTypes = this.documentTypeService.search(this.searchDocumentType);
        component.setAttribute("controller", this);
    }

    public void onClick$buttonOk(Event event) throws Exception {
        if (this.selectedDocumentType != null) {
            this.execution.setAttribute("selectedDocumentType", this.selectedDocumentType);
            this.execution.setAttribute("result", "ok");
            this.self.detach();
        } else {
            Messagebox.show("Please select a Document Type.");
        }
    }

    public void onClick$buttonCancel(Event event) throws Exception {
        this.execution.setAttribute("selectedDocumentType", null);
        this.execution.setAttribute("result", "cancel");
        this.self.detach();
    }
    
    public void onClick$buttonSearch(Event event) throws Exception {
        this.execution.setAttribute("searchDocumentType", this.searchDocumentType);

        Window window = (Window) this.execution.createComponents("~./zul/html/documentTypeSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchDocumentType = (Search) this.execution.getAttribute("searchDocumentType");
        }

        this.documentTypes = this.documentTypeService.search(this.searchDocumentType);
        this.binder.loadAll();
    }
}
